package com.example.zyfypt_321lz.model;

import com.example.zyfypt_321lz.iface.RegisterListener;
import com.example.zyfypt_321lz.iface.Registeriface;
import com.example.zyfypt_321lz.service.RegisterService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * Created by HP on 2017/5/31.
 */


    public class RegisterModel implements Registeriface {

        private Retrofit retrofit;

        private String BASEURL = "http://amicool.neusoft.edu.cn/";

        public RegisterModel(){
            retrofit = new Retrofit.Builder().baseUrl(BASEURL).addConverterFactory(ScalarsConverterFactory.create()).build();
        }

        @Override
        public void getResult(String username, String password, String tel, final RegisterListener registerListener) {
            RegisterService registerService = retrofit.create(RegisterService.class);
            Call<String> call = registerService.reg(username,password,tel);
            call.enqueue(new Callback<String>() {
                @Override
                public void onResponse(Call<String> call, Response<String> response) {
                    if(response.isSuccessful()&&response!=null)
                    {
                        registerListener.onResponse(response.body());
                    }
                    else
                        registerListener.onFail("onresponse fail");
                }

                @Override
                public void onFailure(Call<String> call, Throwable t) {
                    registerListener.onFail(t.toString());
                }
            });
        }

    }